//
//  InformationSee.m
//  WestAir
//
//  Created by s on 16/9/25.
//  Copyright © 2016年 Henry. All rights reserved.
//

#import "InformationSee.h"
#import "InformationSeeTableCell.h"
#import "InforSeeTask.h"
#import "InfoSeeCommission.h"


typedef NS_ENUM(NSInteger, inforMationType) {
    
    renWu  ,
    paiHang,
    tiCheng
};


@interface InformationSee (){


    UIDatePicker  *picker;
    BOOL  isStart;
    UIView  *viewBG;
    
    NSString  *startTimeRenWu;
    NSString  *overTimeRenWu;
    NSString  *HangBianHaoRenWu;
    
    NSString  *startTimeTiCheng;
    NSString  *overTimeTiCheng;
    NSString  *HangBianHaoTiCheng;
    
    NSArray  *arrHangBanRenWu;
    NSArray  *arrHangBanTiCheng;
    
    NSArray  *arrPaiMing;
    
    

    
}

@property(nonatomic,assign)inforMationType  infoType;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeight;


@property (weak, nonatomic) IBOutlet UIView *viewNormal;
@property (weak, nonatomic) IBOutlet UIView *viewRanking;

@property (weak, nonatomic) IBOutlet UIButton *btnSelect;
@property (weak, nonatomic) IBOutlet UIButton *btnStart;
@property (weak, nonatomic) IBOutlet UIButton *btnOver;
@property (weak, nonatomic) IBOutlet UIButton *btnSelectHangBan;
@property (weak, nonatomic) IBOutlet UIView *viewSelectInfor;
@property (weak, nonatomic) IBOutlet UIView *viewSelectSaleTask;


@property(nonatomic,strong) InforSeeTask  *viewTaskBottom;
@property(nonatomic,strong) InfoSeeCommission  *viewCommissionBottom;

@end

@implementation InformationSee

@synthesize viewNormal,viewRanking,tableView,viewSelectInfor,viewSelectSaleTask;



#pragma mark  生命周期
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self  initInforMationSeeUI];
    
   
}


-(void)initInforMationSeeUI{

     float  selectBottom=180;
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"InforSee" owner:self options:nil];
    _viewTaskBottom=nib[0];
    _viewCommissionBottom=nib[1];
    
    [viewNormal  addSubview:_viewTaskBottom];
    [viewNormal  addSubview:_viewCommissionBottom];
  
    
    [_viewTaskBottom  setFrame:CGRectMake(0, selectBottom,viewNormal.widthV, _viewTaskBottom.heightV)];
    [_viewCommissionBottom  setFrame:CGRectMake(0, selectBottom,viewNormal.widthV, _viewCommissionBottom.heightV)];
    _viewCommissionBottom.hidden=YES;
    

    
    self.infoType=renWu;
    
    overTimeRenWu=[self  getDateOfyyyyMMdd:[NSDate  date ]];
    overTimeTiCheng=overTimeRenWu;
    
    NSDate *overDate=[[NSDate  date ]  dateByAddingTimeInterval:-24*7*60*60];
    startTimeRenWu=[self  getDateOfyyyyMMdd:overDate];
    startTimeTiCheng=startTimeRenWu;
    
    [self.btnStart setTitle:startTimeRenWu forState:UIControlStateNormal];
    [self.btnOver  setTitle:overTimeRenWu forState:UIControlStateNormal];
    
    
    
    
    isStart=YES;
    viewRanking.hidden=YES;
    VIEW_CORNER(viewSelectSaleTask, 4.0);
    VIEW_CORNER(viewSelectInfor, 4.0);
    VIEW_CORNER(tableView, 4.0);

    tableView.backgroundColor=[UIColor whiteColor];
    
    VIEW_CORNER(_btnSelect, buttonCorner21);
    _btnSelect.backgroundColor=NORMAL_BULUE_COLOR;
    
    
    viewBG=[[UIView  alloc]  initWithFrame:CGRectMake(0, 0, 300, 266)];
    viewBG.backgroundColor=[UIColor whiteColor];
    
    
    picker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 300, 216)];
    picker.datePickerMode=UIDatePickerModeDate;
    
    VIEW_CORNER(viewBG, 5.0);
    
    UIButton  *btnTem=[[UIButton  alloc]  initWithFrame:CGRectMake(0, picker.bottomV, picker.widthV, 50)];
    [viewBG addSubview:btnTem];
    [btnTem  setTitle:@"确定" forState:UIControlStateNormal];
    [btnTem setTitleColor:NORMAL_BULUE_COLOR forState:UIControlStateNormal];
    
    [btnTem  addTarget:self action:@selector(didSelectQuire:) forControlEvents:UIControlEventTouchUpInside];
    
    [viewBG addSubview:picker];

}


-(void)getPaiHangData{

    NSDictionary  *dicJson=  [CMCCIOTIGUnity   getRequestJson:@{}];
    [[NetworkFactory  alloc]  startPostRequestWithURL:GETSALETOP10 andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
        
            arrPaiMing=responseJson[@"SaleList"];
            
            self.tableViewHeight.constant=(arrPaiMing.count+1)*45;
            tableView.scrollEnabled=NO;
            
            if (arrPaiMing.count*45+84>SCREEN_HEIGHT) {
                
                self.tableViewHeight.constant=SCREEN_HEIGHT-84;
                tableView.scrollEnabled=YES;
                ;
            }
            
            [self.tableView reloadData];
            
        }
        else{
            arrPaiMing=nil;
            self.tableViewHeight.constant=0;

            
        }
        
        
        
    } andFailBlock:^(NSError *error) {
        
        arrPaiMing=nil;
        self.tableViewHeight.constant=0;

        
    } ];
    
    
    


}

#pragma mark  UITableview  deget

#pragma mark Tableview Deleget

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrPaiMing.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray  *arrImage=@[@"q_0.png",@"q_1.png",@"q_2.png",@"q_3.png",@"q_4.png",@"q_5.png"];
    
    InformationSeeTableCell  *cell=(InformationSeeTableCell *)[self.tableView dequeueReusableCellWithIdentifier:@"InformationSee"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSDictionary  *dicTem=arrPaiMing[indexPath.row];
    
    cell.LableStudent.text=dicTem[@"FUserName"];
    [cell.btnPaiMing  setBackgroundImage:[UIImage imageNamed:arrImage[indexPath.row>5?5:indexPath.row]] forState:UIControlStateNormal];
    [cell.btnPaiMing setTitle:[NSString stringWithFormat:@"%ld",[dicTem[@"Seq"]  integerValue]] forState:UIControlStateNormal];
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 45;
    
}


#pragma mark  点击时间

- (IBAction)inforMationSeeType:(id)sender {
    
    UIButton  *btn=(UIButton  *)sender;
    btn.selected=YES;
    
    UIButton  *btnRenWu=[self.view  viewWithTag:100];
    UIButton  *btnPaiHang=[self.view  viewWithTag:101];
    UIButton  *btnTiCheng=[self.view  viewWithTag:102];
    
    if ([btn.titleLabel.text  isEqualToString:@"任务"]) {
        _viewTaskBottom.hidden=NO;
        _viewCommissionBottom.hidden=YES;
        
        viewNormal.hidden=NO;
        viewRanking.hidden=YES;
    
        
        btnPaiHang.selected=NO;
        btnTiCheng.selected=NO;
        self.infoType=renWu;
        
         [_btnStart setTitle:startTimeRenWu?startTimeRenWu:@"起始日期" forState:UIControlStateNormal];
         [_btnOver setTitle:overTimeRenWu?overTimeRenWu:@"结束日期" forState:UIControlStateNormal];
        [_btnSelectHangBan  setTitle:HangBianHaoRenWu?HangBianHaoRenWu:@"选择航班" forState:UIControlStateNormal];
        
    }
    else if ([btn.titleLabel.text  isEqualToString:@"排行"]){
        viewNormal.hidden=YES;
        viewRanking.hidden=NO;
        
        
        btnRenWu.selected=NO;
        btnTiCheng.selected=NO;
        
        self.infoType=paiHang;
        
        [self  getPaiHangData];
    
    }
    else{
        
        _viewTaskBottom.hidden=YES;
        _viewCommissionBottom.hidden=NO;
        
        viewNormal.hidden=NO;
        viewRanking.hidden=YES;
        
        self.infoType=tiCheng;
        
        btnRenWu.selected=NO;
        btnPaiHang.selected=NO;
        
        [_btnStart setTitle:startTimeTiCheng?startTimeTiCheng:@"起始日期" forState:UIControlStateNormal];
        [_btnOver setTitle:overTimeTiCheng?overTimeTiCheng:@"结束日期" forState:UIControlStateNormal];
        [_btnSelectHangBan  setTitle:HangBianHaoTiCheng?HangBianHaoTiCheng:@"选择航班" forState:UIControlStateNormal];
        
    
    }
}


- (IBAction)popBack:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnStart:(id)sender {
    
    isStart=YES;
    [[CMCCIOTIGSharePopWindow  sharedWindow  ]  showWithSubView:viewBG];
     [CMCCIOTIGSharePopWindow  sharedWindow  ].closeType=CMCC_CLOSE_ONLY_BUTTON;
    
}
- (IBAction)btnOver:(id)sender {
      isStart=NO;
      [[CMCCIOTIGSharePopWindow  sharedWindow  ]  showWithSubView:viewBG];
    
      [CMCCIOTIGSharePopWindow  sharedWindow  ].closeType=CMCC_CLOSE_ONLY_BUTTON;
}
- (IBAction)selectHangBan:(id)sender {
    
    NSString  *startDate;
    NSString  *overDate;
    
    if ( self.infoType==renWu ) {
        
        startDate=startTimeRenWu;
        overDate=overTimeRenWu;
    }
    else{
        startDate=startTimeTiCheng;
        overDate=overTimeTiCheng;
    
    }
    
    NSDate  *date1=[self getStrOfyyyyMMdd:startDate];
    NSDate  *date2=[self  getStrOfyyyyMMdd:overDate];
    
    NSComparisonResult  compareResult= [date1  compare:date2];
    if (compareResult==NSOrderedDescending) {
        [CMCCIOTTipsCenter  showErrorWithStatus:@"结束日期 晚于开始日期"];
        return;
    }
    

    
    MemberList  *member=[MemberList  currentUsinfo];
    NSString  *userAccount=member.userAccount;
    
    NSDictionary  *dicAccount=@{@"FUserNo":userAccount,@"FStartDate":startDate,@"FEndDate":overDate};
   NSDictionary  *dicJson= [CMCCIOTIGUnity   getRequestJson:dicAccount];
    
    
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:GETFLIGHTNOLIST andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
        
            if (self.infoType==renWu) {
                
             arrHangBanRenWu=responseJson[@"FlightList"];
                
            }
            else{
            arrHangBanTiCheng=responseJson[@"FlightList"];
            }
            
            
            NSArray  *arrAddressKey=(self.infoType==renWu)?arrHangBanRenWu:arrHangBanTiCheng;
            
            NSMutableArray  *addressKey=[NSMutableArray new];
            //          去掉重复的key
            [arrAddressKey enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                NSString  *key=obj;
                BOOL  isHas=NO;
                for (int i=0; i<addressKey.count; i++) {
                    if ([addressKey[i]  isEqualToString:key]) {
                        isHas=YES;
                        break;
                    };
                }
                if (isHas) {
                    ;
                }
                else{
                    [addressKey addObject:key];
                    
                }
                
                ;
            }];
            if (!addressKey.count) {
                [CMCCIOTTipsCenter showErrorWithStatus:@"暂无航班!"];
                return ;
            }
            
            NSMutableArray *arrAction=[NSMutableArray new];
            NSMutableArray  *arrAddressMutableKey=[[NSMutableArray  alloc]  initWithObjects:@"不限", nil];
            
            [arrAddressMutableKey addObjectsFromArray:addressKey];
            for ( NSInteger i =0; i<arrAddressMutableKey.count; i++) {
                
                NSString     *strHangBan=arrAddressMutableKey[i];
                
                CMCCIOTIGAlertActionCustomer  *actionCustomer=  [CMCCIOTIGAlertActionCustomer  actionCustomerWithTitle:strHangBan style:UIAlertActionCustomeStyleDarkGray  handler:^(CMCCIOTIGAlertActionCustomer * _Nullable action) {
                    
                    
                    if (self.infoType==renWu) {
                        HangBianHaoRenWu= action.title;
                    }
                    else{
                        HangBianHaoTiCheng= action.title;
                        
                    }
                    
                    
                    [ _btnSelectHangBan setTitle:action.title forState:UIControlStateNormal];
                    
                }];
                
                
                
                [arrAction addObject:actionCustomer];
                
            }
            
            [CMCCIOTTipsCenter   showAlertAddressTitle:@"选择航班" message:@"" arrAction:arrAction];
        
        }
        else{
            
            if (self.infoType==renWu) {
                
               arrHangBanRenWu=nil;
                
            }
            else{
                arrHangBanTiCheng=nil;
            }
         
            
        }
        
        
        
    } andFailBlock:^(NSError *error) {
        
        if (self.infoType==renWu) {
            
            arrHangBanRenWu=nil;
            
        }
        else{
            arrHangBanTiCheng=nil;
        }
        
    } ];

    
   

    
}
- (IBAction)select:(id)sender {
 
    NSString  *startDate;
    NSString  *endDate;
    NSString  *hangBan;
    NSString  *method;
    
    if (self.infoType==renWu) {
     
        startDate=startTimeRenWu;
        endDate=overTimeRenWu;
        hangBan=HangBianHaoRenWu;
        method=GETSALETASK;
        ;
    }
    else{
    
        startDate=startTimeTiCheng;
        endDate=overTimeTiCheng;
        hangBan=HangBianHaoTiCheng;
        method=GETSALECOMMISSION;
    
    }
    
    NSDate  *date1=[self getStrOfyyyyMMdd:startDate];
    NSDate  *date2=[self  getStrOfyyyyMMdd:endDate];
    
    NSComparisonResult  compareResult= [date1  compare:date2];
    if (compareResult==NSOrderedDescending) {
        [CMCCIOTTipsCenter  showErrorWithStatus:@"结束日期 晚于开始日期"];
        return;
    }
    
    MemberList  *member=[MemberList  currentUsinfo];
    NSString  *userAccount=member.userAccount;
    if (hangBan) {
        hangBan=[hangBan  isEqualToString:@"不限"]?@"":hangBan;
    }
    else{
    
    hangBan=@"";
    }
    
    NSDictionary  *dicAccount=@{@"FUserNo":userAccount,@"FStartDate":startDate,@"FEndDate":endDate,@"FNUMBER":hangBan};
    
    
    NSDictionary  *dicJson= [CMCCIOTIGUnity   getRequestJson:dicAccount];
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:method andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
        
            if (self.infoType==renWu) {
                
                float  total=[responseJson[@"FSaleTaskAmount"]  floatValue];
                _viewTaskBottom.money.text=[NSString stringWithFormat:@"¥%.1f",total];
                _viewTaskBottom.price.text=[NSString stringWithFormat:@"¥%.1f",[responseJson[@"FSaleMoney"]  floatValue]];
                _viewTaskBottom.lablePercent.text=[NSString stringWithFormat:@"%@",responseJson[@"FPercent"]];
                
                
            }
            else{
                _viewCommissionBottom.money.text=[NSString stringWithFormat:@"¥%.1f",[responseJson[@"FCommissionAmount"] floatValue]]  ;
    
            }
        
        }
        else{
           
        }
       [CMCCIOTTipsCenter  showErrorWithStatus:responseJson[@"message"]];
        
    } andFailBlock:^(NSError *error) {
    
    
    
    }];


}

-(NSString *)getDateOfyyyyMMdd:(NSDate *)inputDate{
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *str = [outputFormatter stringFromDate:inputDate];
    return str;
    
}

-(NSDate  *)getStrOfyyyyMMdd:(NSString *)inputDate{
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *str = [outputFormatter dateFromString:inputDate];
    return str;
    
}


-(void)didSelectQuire:(UIButton  *)btnTem{

   [[CMCCIOTIGSharePopWindow  sharedWindow  ]  dismiss];
    NSDate  *selectDate=  picker.date;
    NSString  *strDate=[self  getDateOfyyyyMMdd:selectDate ];
    if (isStart) {
        [_btnStart  setTitle:strDate forState:UIControlStateNormal];
        if (self.infoType==renWu) {
             startTimeRenWu=  strDate;
          
        }
        else{
             startTimeTiCheng=strDate  ;
        }
       
    }
    else{
        [_btnOver  setTitle:strDate forState:UIControlStateNormal];
        
        if (self.infoType==renWu) {
            
            overTimeRenWu=  strDate;
        }
        else{
            overTimeTiCheng=  strDate;
        }

       
        
    }

 
}

@end
